package com.smart.lib.service;

import com.smart.lib.entity.BorrowOrderInfo;
import com.smart.lib.entity.UserBorrowInfo;
import com.smart.lib.entity.UserInfo;
import com.smart.lib.mapper.UserMapper;
import com.smart.lib.util.Pager;
import com.smart.lib.util.PagerData;
import com.smart.lib.util.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.HashMap;

/**
 * 学生用户信息管理实现类
 * */
@Service
public class UserService {
    @Autowired
    UserMapper userMapper;

    /**
     * 查询学生信息分页数据
     * 搜索条件：学生姓名和借阅卡号
     * */
    private List<UserInfo> getAllUsersPagerByUsernameOrCardNo(Pager pager){
        Integer currentPage = pager.getCurrentPage();
        Integer startIndex = (currentPage - 1)*pager.getPageSize();
        pager.setStartIndex(startIndex);
        return userMapper.getAllUsersPagerByUsernameOrCardNo(pager);
    }

    /**
     * 学生读者分页查询
     * 查询字段 @username,@cardno
     * @return 获取条件查询数据条数，用于计算分页页码
     * */
    private Integer getAllUsersPagerTotalCountsByUsernameOrCardNo(Pager pager){
        return userMapper.getAllUsersPagerTotalCountsByUsernameOrCardNo(pager);
    }

    /**
     * 手动计算页码
     * */
    private Integer getPageTotalByUsernameOrCardNo(Pager pager){
        Integer userTotalCounts = getAllUsersPagerTotalCountsByUsernameOrCardNo(pager);
        Integer pageTotal = 1; //初始设定当前只有一页
        //数据总条数除以pageSize每页显示条数，如取余数求模，如果有余数，页码+1
        //如果可以整除，则页码等于数据总数除以pageSize
        pageTotal = userTotalCounts/pager.getPageSize();
        if(userTotalCounts%pager.getPageSize()>0){
            pageTotal = pageTotal + 1;
        }
        return pageTotal;
    }

    /**
     * 将根据模糊查询得到的分页数据和计算得到的页码传给前端使用
     * 进行数据渲染
     * */
    public Response getAllUserPagerDataByUsernameOrCardNo(Pager pager){
        PagerData pagerData = new PagerData();
        pagerData.setData(getAllUsersPagerByUsernameOrCardNo(pager));
        pagerData.setPageTotal(getPageTotalByUsernameOrCardNo(pager));
        pagerData.setTotal(getAllUsersPagerTotalCountsByUsernameOrCardNo(pager));
        return new Response(200,"查询学生用户信息分页成功",pagerData);
    }

    /**
     * 更新用户头像
     */
    public Response updateUserAvatar(UserInfo userInfo) {
        try {
            int result = userMapper.updateUserAvatar(userInfo);
            if (result > 0) {
                return new Response(200, "头像更新成功", userInfo.getStudent_img());
            }
            return new Response(401, "头像更新失败", null);
        } catch (Exception e) {
            return new Response(500, "系统错误：" + e.getMessage(), null);
        }
    }

    /**
     * 添加新学生信息
     */
    public Response addNewUserInfo(UserInfo userInfo) {
        try {
            Integer error = userMapper.addNewUserInfo(userInfo);
            // 通过存储过程的输出参数获取结果
            if (error == 0) {
                return new Response(200, "添加学生成功", null);
            }
            return new Response(401, "添加学生失败", null);
        } catch (Exception e) {
            return new Response(500, "系统错误：" + e.getMessage(), null);
        }
    }

    /**
     * 借阅管理流程中，通过图书证二维码查询当前用户信息
     * 包括用户借阅订单内容
     * 整合信息
     * */
    public Response getUserBorrowInfoByCardno(UserInfo userInfo){
        //根据图书证查询用户信息
        UserBorrowInfo userBorrowInfo = userMapper.getUserInfoByCardNo(userInfo);
        if(userBorrowInfo!=null){
            //根据图书证获取用户借阅图书数量
            Integer borrowedCounts = userMapper.getUserBorrowingBookCountsByCardNo(userInfo);
            userBorrowInfo.setBorrowedCount(borrowedCounts);
            //根据图书证获取用户已借阅图书书籍
            List<BorrowOrderInfo> borrowingBooks = userMapper.getUserBorrowingBooksByCardNo(userInfo);
            userBorrowInfo.setBorrowingBooks(borrowingBooks);
            return new Response(200,"查询用户借阅信息成功",userBorrowInfo);
        }else{
            return new Response(401,"查询失败，当前用户不存在",null);
        }
    }

    /**
     * 当前学生总数
     * */
    public Integer getUserTotal(){
        return userMapper.getUserTotal();
    }
}
